package dao

import (
	"context"
	"device-admin/internal/dal"
	"device-admin/internal/models"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type MaterialAuditLog struct{}

func (MaterialAuditLog) Create(ctx context.Context, db *gorm.DB, materialID, merchantID, auditUser, auditUserName string,
	auditStatus int) (res []*models.MaterialAuditLog, total int64, err error) {
	m := dal.Use(db).MaterialAuditLog

	var data models.MaterialAuditLog
	data.ID, _ = helper.UUID{}.String()
	data.MaterialID = materialID
	data.MerchantID = merchantID
	data.AuditUser = auditUser
	data.AuditUsername = auditUserName
	data.AuditStatus = auditStatus
	m.WithContext(ctx).Create(&data)
	return
}

type AuditLogList struct {
	models.MaterialAuditLog
	Type              int    `gorm:"column:type" json:"type"`
	Name              string `gorm:"column:name" json:"-"`
	Content           string `gorm:"column:content" json:"content"`
	FilePath          string `gorm:"column:file_path" json:"file_path"`
	CreateUser        string `gorm:"column:create_user" json:"create_user"`
	CreateUsername    string `gorm:"column:create_username" json:"create_username"`
	MerchantName      string `gorm:"-" json:"merchant_name"`
	AuditMerchantName string `gorm:"-" json:"audit_merchant_name"`
}

func (MaterialAuditLog) List(ctx context.Context, db *gorm.DB, page, size int) (res []AuditLogList, total int64, err error) {
	log := dal.Use(db).MaterialAuditLog
	m := dal.Use(db).Material
	offset, limit := GetOffsetLimit(page, size)
	log.WithContext(ctx).
		Join(m, m.ID.EqCol(log.MaterialID)).
		Select(log.ALL, m.Type, m.Name, m.Content, m.FilePath, m.CreateUser, m.CreateUsername).
		Order(log.MaterialID.Desc()).
		ScanByPage(&res, offset, limit)
	return
}
